
import {getAuthority} from '../../api'
import {message} from "../../common/util";


const state = {//数据
  authority: {},
  authoritySubscribe:{},
  authorityPresent:{},
  authorityNotice:{},
  authorityPerformance:''
}

const getters = {//获取数据
  /*getAuthority: function (state) {
    return state.authority
  }*/
  getAuthority: state => state.authority,//es6箭头函数写法
  getAuthoritySubscribe: state => state.authoritySubscribe,
  getAuthorityPresent: state => state.authorityPresent,
  getAuthorityNotice: state => state.authorityNotice,
  getAuthorityPerformance: state => state.authorityPerformance
}

const mutations = {//对本地无需ajax的更改，使用mutations
  updateAuthority(state,changed){
    state.authority = changed
  },
  updateAuthoritySubscribe(state,changed){
    state.authoritySubscribe = changed
  },
  updateAuthorityPresent(state,changed){
    state.authorityPresent = changed
  },
  updateAuthorityNotice(state,changed){
    state.authorityNotice = changed
  },
  updateAuthorityPerformance(state,changed){
    state.authorityPerformance = changed
  }

}

const actions = {//action好处适合异步操作，action先执与后端交互，mutations后执行
  fetchAuthority({commit,state},params){
    getAuthority().then((res)=>{
      if(res.status == 'success')  {
        commit('updateAuthority',res.data)
        let subscribeManagerValue = {viewValue:'self', hostessValue:''},
          presentManagerValue = {viewValue:'self', girlValue:''},
          subscribePerformanceValue = 'self',
          noticeValue = {callServiceValue:'local',presentValue:'',keepValue:'',cardValue:''}
        for(let authItem of res.data.h5_saleHelper){
          if(authItem.subscribeManager != undefined){
            for(let item of authItem.subscribeManager){
              if(typeof item == 'string' && item == 'subscribe_hostess'){
                subscribeManagerValue.hostessValue = 'manager'
              }
              if(typeof item == 'string' && item == 'subscribe_manager'){
                subscribeManagerValue.viewValue = 'manager'
              }else if(typeof item == 'string' && item == 'subscribe_depart' && subscribeManagerValue.viewValue != 'manager'){
                subscribeManagerValue.viewValue = 'depart'
              }
            }
          }
          if(authItem.presentManager != undefined){
            for(let item of authItem.presentManager){
              if(typeof item == 'string' && item == 'present_girl'){
                presentManagerValue.girlValue = 'manager'
              }
              if(typeof item == 'string' && item == 'present_manager'){
                presentManagerValue.viewValue = 'manager'
              }else if(typeof item == 'string' && item == 'present_depart' && presentManagerValue.viewValue != 'manager'){
                presentManagerValue.viewValue = 'depart'
              }
            }
          }
          if(authItem.notice != undefined){
            for(let item of authItem.notice){
              if(typeof item == 'object' && item.callService == 'notice_global'){
                noticeValue.callServiceValue = 'global'
              }
              if(typeof item == 'string' && item == 'notice_present_audit_all'){
                noticeValue.presentValue = 'present_audit_all'
              }else if(typeof item == 'string' && item == 'notice_present_audit' && noticeValue.presentValue != 'present_audit_all'){
                noticeValue.presentValue = 'present_audit'
              }
              if(typeof item == 'string' && item == 'notice_keep_audit'){
                noticeValue.keepValue = 'keep_audit'
              }
              if(typeof item == 'string' && item == 'notice_card_audit'){
                noticeValue.cardValue = 'card_audit'
              }
            }
          }
          if(authItem.subscribePerformance != undefined){
            for(let item of authItem.subscribePerformance){
              if(typeof item == 'string' && item == 'performance_manager'){
                subscribePerformanceValue = 'manager'
              }else if(typeof item == 'string' && item == 'performance_depart' && subscribePerformanceValue != 'manager'){
                subscribePerformanceValue = 'depart'
              }
            }
          }
        }
        //console.log(subscribeManagerValue,presentManagerValue,subscribePerformanceValue,noticeValue)
        commit('updateAuthoritySubscribe',{view:subscribeManagerValue.viewValue,hostess:subscribeManagerValue.hostessValue})
        commit('updateAuthorityPresent',{view:presentManagerValue.viewValue,girlPresent:presentManagerValue.girlValue})
        commit('updateAuthorityPerformance',subscribePerformanceValue)
        commit('updateAuthorityNotice',{
          callServices:noticeValue.callServiceValue,
          present:noticeValue.presentValue,
          keep:noticeValue.keepValue,
          card:noticeValue.cardValue
        })
      }else if(res.status == 'error'){
        console.log('getAuthority error info:',res.msg)
        message({message:res.msg,type: 'error'})
      }
      if(params.callback != undefined)params.callback(res.status)
    }).catch((err)=>{console.log('getAuthority catch info:',err)})
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
